package com.roc.util;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

public class IDGenerator {

	private static int count = 0;

	private static NumberFormat nf = NumberFormat.getIntegerInstance();

	static {
		nf.setMaximumIntegerDigits(5);
		nf.setMinimumIntegerDigits(5);
		nf.setGroupingUsed(false);
	}

	public static synchronized String get18ID() {
		String result = "";
		result = System.currentTimeMillis() + nf.format(count++);
		if (count > 99999) {
			count = 0;
			try {
				TimeUnit.MILLISECONDS.sleep(5);
			} catch (InterruptedException e) {
				// e.printStackTrace();
			}
		}
		return result;
	}

	public static void main(String[] args) {
		String temp = "";
		List<String> ids = new ArrayList<String>();
		long begin = System.currentTimeMillis();
		for (int i = 0; i < 100000; i++) {
			ids.add(get18ID());
			// System.out.println(IDGenerator.get18ID());
		}
		long end = System.currentTimeMillis();
		System.out.println("取ID：" + (end - begin) * 1.0 / 1000);
		List<String> check = new ArrayList<String>();
		int count = 0;
		for (String id : ids) {
			if (check.contains(id)) {
				System.out.println("double:" + id);
				count++;
			}
			if (id.length() != 18) {
				System.out.println("error:" + id);
			}
			check.add(id);
		}
		System.out.println("double count: " + count);
		// System.out.println(IDGenerator.get18ID());
		/*
		 * while(true){ String id = IDGenerator.get18ID();
		 * 
		 * // System.out.println(id); if(id.length()!= 18){ break; }
		 * if(temp.equals(id)){ break; } temp=id; }
		 */
	}
}
